package com.live.shop.sql;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.live.shop.model.IpconfigModel;

public class IpconfigDatabase {
	private static MyDatabaseHelper helper;
	public IpconfigDatabase(Context context){
		//创建数据库和表
		helper = new MyDatabaseHelper(context, "user.db",null,3,null);
	}
	@SuppressLint("Range")
	public static IpconfigModel select(){
		IpconfigModel model= new IpconfigModel();
		SQLiteDatabase database = helper.getReadableDatabase();
		Cursor cursor = database.rawQuery("select * from ipconfig where id = 1", null);
		if (cursor != null && cursor.getCount() > 0) {
			if (cursor.moveToNext()) {
				model.setId(cursor.getInt(cursor.getColumnIndex("id")));
				model.setServerIp(cursor.getString(cursor.getColumnIndex("server_ip")));
				model.setServerPort(cursor.getString(cursor.getColumnIndex("server_port")));
			}
			cursor.close();
		}
		database.close();
		return model;
	}
	//更改服务端配置
	public static void updateServer(String server_ip,String server_port) {
		SQLiteDatabase database = helper.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("server_ip", server_ip);
		values.put("server_port", server_port);
		int updateNum = database.update("ipconfig", values, "id = ?", new String[] {"1"});
		if(updateNum <= 0){
			//如果受影响的行数为0 代表没有 则新增
			database.insert("ipconfig", null, values); // 插入一条数据
		}
		values.clear();
		database.close();
	}
}
